package com.tl.boot.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tl.boot.common.annotation.RestControllerRequstMapping;
import com.tl.boot.common.annotation.WithPermission;
import com.tl.boot.common.constants.CommonConstant;
import com.tl.boot.common.enums.BooleanEnum;
import com.tl.boot.common.utils.PermissionUtil;
import com.tl.boot.common.vo.PageInfoVO;
import com.tl.boot.common.vo.ResponseVO;
import com.tl.boot.entity.menu.domain.MenuAfterLoginInfo;
import com.tl.boot.entity.menu.dto.AddMenuDTO;
import com.tl.boot.entity.menu.dto.ListMenuInfoDTO;
import com.tl.boot.entity.menu.dto.UpdateMenuBindResourceDTO;
import com.tl.boot.entity.menu.dto.UpdateMenuInfoDTO;
import com.tl.boot.entity.menu.form.*;
import com.tl.boot.entity.menu.vo.ListMenuInfoVO;
import com.tl.boot.entity.menu.vo.MenuAfterLoginVO;
import com.tl.boot.entity.menu.vo.QueryMenuInfoVO;
import com.tl.boot.service.menu.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * com.tl.boot.controller -- MeauController
 * Description:
 * Created by joshua_liu <a href="mailto:joshualwork@163.com"></a>
 * On 2019/9/16 11:07
 */
@Api(tags = "菜单模块")
@RestControllerRequstMapping("auth/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;


    @ApiOperation( value = "添加菜单信息", notes = "传添加菜单信息请求对象" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "addMenuForm", value = "添加菜单信息请求对象", required = true,
                    dataType = "AddMenuForm", paramType = "body" ),
            @ApiImplicitParam( name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header" ),
    } )
    @PostMapping("add")
    @WithPermission(role = {"sysadmin"})
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    public ResponseVO addMenuInfo(@RequestBody AddMenuForm addMenuForm) {
        AddMenuDTO dto = new AddMenuDTO();
        BeanUtils.copyProperties(addMenuForm, dto);
        dto.setIsLeaf(addMenuForm.getIsLeafEnum().getCode().shortValue());
        dto.setMenuStatus(addMenuForm.getMenuStatusEnum().getCode().shortValue());
        Long result = menuService.addMenuInfo(dto);
        return ResponseVO.buildSuccess(result);
    }

    @PostMapping("delete")
    @WithPermission(role = {"sysadmin"})
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    @ApiOperation( value = "删除菜单信息", notes = "传删除菜单信息请求对象" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "form", value = "删除菜单信息请求对象", required = true,
                    dataType = "DelMenuInfoForm", paramType = "body" ),
            @ApiImplicitParam( name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header" ),
    } )
    public ResponseVO delMenuInfo(@RequestBody DelMenuInfoForm form) {
        menuService.delMenuInfo(form.getMenuId());
        return ResponseVO.buildSuccess();
    }

    @PostMapping("update")
    @WithPermission(role = {"sysadmin"})
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    @ApiOperation( value = "更新菜单信息", notes = "传更新菜单信息请求对象" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "form", value = "更新菜单信息请求对象", required = true,
                    dataType = "UpdateMenuInfoForm", paramType = "body" ),
            @ApiImplicitParam( name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header" ),
    } )
    public ResponseVO updateMenuInfo(@RequestBody UpdateMenuInfoForm form) {
        UpdateMenuInfoDTO dto = new UpdateMenuInfoDTO();
        BeanUtils.copyProperties(form,dto);
        dto.setMenuStatus(form.getMenuStatusEnum().getCode().shortValue());
        dto.setIsLeaf(form.getIsLeafEnum().getCode().shortValue());
        menuService.updateMenuInfo(dto);
        return ResponseVO.buildSuccess();
    }

    @PostMapping("bindResourceUpdate")
    @WithPermission(role = {"sysadmin"})
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    @ApiOperation( value = "更新菜单绑定资源信息", notes = "传更新菜单绑定资源信息请求对象" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "form", value = "更新菜单绑定资源信息请求对象", required = true,
                    dataType = "UpdateMenuBindResourceForm", paramType = "body" ),
            @ApiImplicitParam( name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header" ),
    } )
    public ResponseVO updateMenuBindResource(@RequestBody UpdateMenuBindResourceForm form) {
        UpdateMenuBindResourceDTO dto = new UpdateMenuBindResourceDTO();
        BeanUtils.copyProperties(form,dto);
        Long mrId = menuService.updateMenuBindResource(dto);
        return ResponseVO.buildSuccess(mrId);
    }

    @PostMapping("query")
    @ApiOperation( value = "获取菜单详细信息", notes = "传获取菜单详细信息请求对象" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "form", value = "获取菜单详细信息请求对象", required = true,
                    dataType = "QueryMenuInfoForm", paramType = "body" ),
            @ApiImplicitParam( name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header" ),
    } )
    public ResponseVO<QueryMenuInfoVO> queryMenuInfo(@RequestBody QueryMenuInfoForm form) {
        QueryMenuInfoVO queryMenuInfoVO = menuService.queryMenuInfo(form.getMenuId());
        return ResponseVO.buildSuccess(queryMenuInfoVO);
    }


    @ApiOperation( value = "获取菜单列表信息", notes = "传获取菜单列表信息请求对象" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "form", value = "获取菜单列表信息请求对象", required = true,
                    dataType = "ListMenuInfoForm", paramType = "body" ),
            @ApiImplicitParam( name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header" ),
    } )
    @PostMapping("list")
    public ResponseVO<PageInfoVO<ListMenuInfoVO>> listMenuInfo(@RequestBody ListMenuInfoForm form) {
        PageHelper.startPage(form.getPage(), form.getSize());
        ListMenuInfoDTO dto = new ListMenuInfoDTO();
        BeanUtils.copyProperties(form,dto);
        List<ListMenuInfoVO> menuInfoVOS = menuService.listMenuInfo(dto);
        Page<ListMenuInfoVO> page = (Page<ListMenuInfoVO>) menuInfoVOS;
        return ResponseVO.buildSuccess(page.getResult(),page.getTotal());
    }

    @ApiOperation( value = "登录后获取动态菜单信息", notes = "传获取动态菜单信息请求对象" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "meauAfterLoginInfoForm", value = "获取动态菜单信息请求对象", required = true,
                    dataType = "MeauAfterLoginInfoForm", paramType = "body" ),
            @ApiImplicitParam( name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header" ),
    } )
    @PostMapping("menuAfterLoginInfo")
    public ResponseVO<List<MenuAfterLoginVO>> meauAfterLoginInfo(@RequestBody MeauAfterLoginInfoForm meauAfterLoginInfoForm) {
        JSONObject obj = PermissionUtil.getPermissionInfo(meauAfterLoginInfoForm.getUserId());
        List<MenuAfterLoginInfo> menuAfterLoginInfoList = JSON.parseArray(
                JSONObject.toJSONString(obj.get(CommonConstant.MENU_INFO_LIST)), MenuAfterLoginInfo.class);
        List<MenuAfterLoginVO> menuAfterLoginVOS = menuAfterLoginInfoList.stream().map(e -> {
            MenuAfterLoginVO menuAfterLoginVO = new MenuAfterLoginVO();
            BeanUtils.copyProperties(e, menuAfterLoginVO);
            menuAfterLoginVO.setIsLeafEnum(BooleanEnum.getInstanceByCode(e.getIsLeaf().intValue()));
            return menuAfterLoginVO;
        }).collect(Collectors.toList());
        return ResponseVO.buildSuccess(menuAfterLoginVOS);
    }

}
